package com.hirsi.module.role.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.hirsi.core.expand.TableRUpdate;
import com.hirsi.module.manager.entity.RManagerRole;
import com.hirsi.module.manager.service.RManagerRoleService;
import com.hirsi.module.role.convert.RoleConvert;
import com.hirsi.module.role.entity.RRoleMenu;
import com.hirsi.module.role.entity.Role;
import com.hirsi.module.role.mapper.RoleMapper;
import com.hirsi.module.role.service.RRoleMenuService;
import com.hirsi.module.role.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hirsi.module.role.vo.EditRoleVo;
import com.hirsi.module.role.vo.param.SaveRolePermissionParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author Hirsi
 * @since 2024-03-11
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RManagerRoleService managerRoleService;

    @Autowired
    private RRoleMenuService roleMenuService;

    @Override
    public EditRoleVo edit(Integer id) {
        Role role = lambdaQuery().eq(Role::getId, id)
                .select(Role::getId, Role::getCode, Role::getName, Role::getIsSuper, Role::getStatus)
                .one();
        if (role == null) throw new RuntimeException("角色不存在");
        return RoleConvert.INSTANCE.roleToEditRoleVo(role);
    }

    @Override
    public void save(EditRoleVo vo) {
        Role role = RoleConvert.INSTANCE.editRoleVoToRole(vo);
        role.insertOrUpdate();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<Integer> ids) {
        removeByIds(ids);
        //删除管理员部门中间数据
        managerRoleService.lambdaUpdate().in(RManagerRole::getRoleId, ids).remove();
    }

    @Override
    public List<Integer> permission(Integer id) {
        return roleMenuService.lambdaQuery().eq(RRoleMenu::getRoleId, id)
                .select(RRoleMenu::getMenuId).list()
                .stream().map(RRoleMenu::getMenuId).distinct().collect(Collectors.toList());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void savePermission(SaveRolePermissionParam param) {
        Long count = lambdaQuery().eq(Role::getId, param.getId()).count();
        if (count == 0) throw new RuntimeException("角色不存在");
        List<RRoleMenu> roleMenus = param.getMenuIds().stream().distinct().map(item -> new RRoleMenu().setRoleId(param.getId()).setMenuId(item)).collect(Collectors.toList());
        List<RRoleMenu> oldRoleMenus = roleMenuService.lambdaQuery().eq(RRoleMenu::getRoleId, param.getId())
                .select(RRoleMenu::getId, RRoleMenu::getRoleId, RRoleMenu::getMenuId).list();
        TableRUpdate.Result<RRoleMenu> execute = TableRUpdate.execute(roleMenus, oldRoleMenus);
        if (CollUtil.isNotEmpty(execute.getSaves())) roleMenuService.saveOrUpdateBatch(execute.getSaves());
        if (CollUtil.isNotEmpty(execute.getDeleteIds())) roleMenuService.removeByIds(execute.getDeleteIds());
    }
}
